<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/29/029
 * Time: 9:10
 */

namespace app\index\controller;


use app\common\redis\RedisPackage;

class JiaoYi extends Common
{
    /**
     * 持仓
     */
    public function chiCang(){
        $id = $this->id;
        if($this->isCZY){
            $id = $this->jujian['par_id'];
        }
        $data = db('order_jl_num')
            ->alias('o')
            ->field('o.*,y.id as uid,y.bh,hy.name as heyue,hb.name as bizhong')
            ->join('yonghu y', 'o.yh_id=y.id')
            ->join('heyue hy', 'o.heyue_id=hy.id')
            ->join('huobi hb', 'o.hb_id=hb.id')
            ->whereLike('y.sj_xulie', '%,' .  $id . ',%')
            ->where('o.bz', 1)
            ->select();
        returnSuccess($data);
    }
    /**
     * 单个用户持仓
     */
    public function oneChiCang(){
        $id = input('id/d');
        if($id> 0){
            $data = db('order_jl_num')
                ->alias('o')
                ->field('o.*,y.id as uid,y.bh,hy.name as heyue,hb.name as bizhong,y.xingming')
                ->join('yonghu y', 'o.yh_id=y.id')
                ->join('heyue hy', 'o.heyue_id=hy.id')
                ->join('huobi hb', 'o.hb_id=hb.id')
                ->where('y.id', $id)
                ->where('o.bz', 1)
                ->select();
            returnSuccess($data);
        }else{
            returnError('请选择用户！');
        }
    }



    /**
     * 成交
     */
    public function chengJiao(){
        $id = $this->id;
        if($this->isCZY){
            $id = $this->jujian['par_id'];
        }
        RedisPackage::getInstance();
        $duilie = 'ib_' . $id . '_rev';
        $jiaoyi = RedisPackage::lPop($duilie);
        $jiaoyi =  $jiaoyi ?  $jiaoyi : [];
        if($jiaoyi){
            $jiaoyi = explode(';', $jiaoyi);
            $yh_id = db('order_jl')->where('id', $jiaoyi[2])->value('yh_id');
            $zhanghao = db('yonghu')->where('id', $yh_id)->value('bh');
            $jiaoyi['zhanghao'] = $zhanghao;
        }
        returnSuccess($jiaoyi);
    }

    /**
     *一键全平
     */

    public function yijianquanping(){
        $gid = $this->id;
        if($this->isCZY){
            returnError('操作员无权限');
        }
        returnSuccess('');die;
        RedisPackage::getInstance();
        // 17;柜员代理ID;用户ID;合约ID;合约方向;数量;明细ID;
        $data = "17;{$gid};0;0;0;0;0;";
        if(RedisPackage::rpush('qiang_ping', $data)){
            returnSuccess('');
        }else{
            returnError('出现错误！');
        }
    }
    // 账号全平
    public function zhanghaoquanping()
    {
        $gid = $this->id;
        if($this->isCZY){
            returnError('操作员无权限');
        }
        returnSuccess('');die;
        $uid = input('uid/d');
        $id = input('id/d');
        RedisPackage::getInstance();
        // 17;柜员代理ID;用户ID;合约ID;合约方向;数量;明细ID;
        $data = "17;{$gid};{$uid};0;0;0;{$id};";
        if(RedisPackage::rpush('qiang_ping', $data)){
            returnSuccess('');
        }else{
            returnError('出现错误！');
        }
    }
    // 分手强平
    public function fenshouqiangping()
    {
        RedisPackage::getInstance();
        $gid = $this->id;
        if($this->isCZY){
            returnError('操作员无权限');
        }
        returnSuccess('');die;
        $uid = input('uid/d');
        $heyue_id = input('heyue_id/d');
        $fangxiang = input('fangxiang/d');
        $num =input('num/d');
        $id = input('id/d');
        // 17;柜员代理ID;用户ID;合约ID;合约方向;数量;明细ID;
        $data = "17;{$gid};{$uid};{$heyue_id};{$fangxiang};{$num};{$id};";
        if(RedisPackage::rpush('qiang_ping', $data)){
            returnSuccess('');
        }else{
            returnError('出现错误！');
        }
    }


}